package com.zq.it.cms.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;

import com.zq.it.cms.common.BaseEntity;
import com.zq.it.cms.common.util.Encodes;

/**
 * @author zhengqiang
 * @description 用户表
 */
@Entity
@Table(name = "cms_user")
public class User extends BaseEntity{
	
	@Column(columnDefinition = "varchar(255) COMMENT '用户名'")
	private   String           userName;
	@Column(columnDefinition = "varchar(255) COMMENT '用户密码'")
	private   String           password;
	@Column(columnDefinition = "int(1) COMMENT '是否锁定'")
	private   boolean          locked;
	@Column(columnDefinition = "int(1) COMMENT '是否激活'")
	private   boolean          active;
	@Column(columnDefinition = "varchar(255) COMMENT '加密salt'")
	private   String           salt;
	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "user")
	@OrderColumn(name = "roleIndex")
	private   List<UserRole>   roles = new ArrayList<UserRole>();    
	
	@Transient
	public byte[] getSaltBytes() {
		String salt = getSalt();
		if (StringUtils.isNotBlank(salt)) {
			return Encodes.decodeHex(salt);
		} else {
			return null;
		}
	}
	
	/**
	 * 获取角色列表
	 */
	@Transient
	public List<Role> getRoleList(){
		List<Role> roleList = new ArrayList<Role>();
		for(UserRole userRole : getRoles()){
			roleList.add(userRole.getRole());
		}
		return roleList;
	}
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public boolean isLocked() {
		return locked;
	}
	public void setLocked(boolean locked) {
		this.locked = locked;
	}
	public boolean isActive() {
		return active;
	}
	public void setActive(boolean active) {
		this.active = active;
	}
	public String getSalt() {
		return salt;
	}
	public void setSalt(String salt) {
		this.salt = salt;
	}
	public List<UserRole> getRoles() {
		return roles;
	}
	public void setRoles(List<UserRole> roles) {
		this.roles = roles;
	}
}
